package com.fulu.netty.luban;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.util.Arrays;

public class TestByteBuf {
    public static void main(String[] args) {
        ByteBuf buffer = Unpooled.buffer(10);
        System.out.println(buffer.toString());
        System.out.println(Arrays.toString(buffer.array()));

        // 写入一段内容 写指针到5
        byte[] bs1 = {1, 2, 3, 4, 5};
        buffer.writeBytes(bs1);
        System.out.println(buffer.toString());
        System.out.println(Arrays.toString(buffer.array()));

        // 读取一段内容 读指针到2
        byte b1 = buffer.readByte();
        byte b2 = buffer.readByte();
        System.out.println(Arrays.toString(new byte[]{b1, b2}));
        System.out.println(buffer.toString());
        System.out.println(Arrays.toString(buffer.array()));

        // 将已读内容丢弃 读指针到0 ，写指针到3
        buffer.discardReadBytes();
        System.out.println(buffer.toString());
        System.out.println(Arrays.toString(buffer.array()));

        // 清空指针数据 读写指针都到0
        buffer.clear();
        System.out.println(buffer.toString());
        System.out.println(Arrays.toString(buffer.array()));

        // 再次写入数据
        byte[] bs2 = {1, 2, 3};
        buffer.writeBytes(bs2);
        System.out.println(buffer.toString());
        System.out.println(Arrays.toString(buffer.array()));

        // 清空buffer数据 不会清除指针
        buffer.setZero(0, buffer.capacity());
        System.out.println(buffer.toString());
        System.out.println(Arrays.toString(buffer.array()));

        // 再次写入，扩容到64
        byte[] bs3 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
        buffer.writeBytes(bs3);
        System.out.println(buffer.toString());
        System.out.println(Arrays.toString(buffer.array()));


    }
}
